package com.mobjam.workingtitleforagame;

import java.util.Timer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MapView extends View {

	Paint paint;
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	protected float x;
	protected float y;
	protected float scaleFactor;
	Bitmap regionMap;
	boolean zoomedIn;
	private int tileWidth;
	private int tileHeight;
	private Matrix matrix;
	private boolean animated = false;
	
	private boolean characterIsMoving = false;
	private int destinationColumn = 0;
	private int destinationRow = 0;
	private float lastZoomedOutX;
	private float lastZoomedOutY;
	
	public float getX( ) { return x; }
	public float getY( ) { return y; }
	
	public void restartAnimation( )
	{
		getHandler().removeCallbacks(updateAnimation);
		getHandler().postDelayed(updateAnimation, 5 );
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		if( regionMap != null && matrix != null )
		{
			canvas.drawBitmap(regionMap,matrix,paint);
		}
		else
		{
			canvas.drawText( "daneel olivaw", x, y, paint);
		}	
		if( !animated )
		{
			animated = true;
			getHandler().removeCallbacks(updateAnimation);
			getHandler().postDelayed(updateAnimation, 5 );
		}
	}

//	private long updateTime;
	private Runnable updateAnimation = new Runnable() {
		private boolean animationLocked = false;
			public void run()
			{
				if( animationLocked ) return;
				animationLocked = true;
				
				if( getWindowVisibility() != VISIBLE )
				{
					animationLocked = false;
					return;
				}
				
				if( characterIsMoving )
				{
					boolean characterMoved = false;
					if( destinationColumn > gameMap.getCharacter().getCurrentTile().getInRegionX())
					{
						characterMoved = gameMap.moveCharacterEast();
					}
					else if( destinationColumn < gameMap.getCharacter().getCurrentTile().getInRegionX() )
					{
						characterMoved = gameMap.moveCharacterWest();
					}
					
					if( destinationRow > gameMap.getCharacter().getCurrentTile().getInRegionY() )
					{
						characterMoved |= gameMap.moveCharacterSouth();
					}
					else if( destinationRow < gameMap.getCharacter().getCurrentTile().getInRegionY() )
					{
						characterMoved |= gameMap.moveCharacterNorth();
					}
					
					if( !characterMoved )
					{
						characterIsMoving = false;
					}
				}
				
				gameMap.getCurrentRegion().incrementAnimationFrame();
				invalidate();
				animationLocked = false;
				getHandler().postDelayed(this, 50 );
			}
	};

	protected void scrollX(float distance )
	{
		x += distance;
		matrix.postTranslate(distance, 0);
		this.invalidate();
	}
	
	public void scrollY(float distance )
	{
		y += distance;
		matrix.postTranslate(0, distance);
		this.invalidate();
	}
	
	public void zoomIn( float x, float y)
	{
		if( !setOffsets )
		{
			int [] location = new int[2];;
			this.getLocationOnScreen(location);
			viewOffsetX = (float)location[0];
			viewOffsetY = (float)location[1];
			setOffsets = true;
		}
		
		int hitColumn = (int)( (x - this.x - viewOffsetX ) / ( this.tileWidth * scaleFactor ) );
		int hitRow = (int)( (y - this.y - viewOffsetY ) / ( this.tileHeight * scaleFactor ) );
				
		scaleFactor = 4;
		if( !zoomedIn )
		{
			lastZoomedOutX = this.x;
			lastZoomedOutY = this.y;
		}
		this.x = x - this.viewOffsetX - hitColumn * scaleFactor * tileWidth;
		this.y = y - this.viewOffsetY - hitRow * scaleFactor*tileHeight;;
		matrix.reset();
		matrix.postScale(scaleFactor, scaleFactor);
		matrix.postTranslate(this.x,this.y);
		zoomedIn = true;
		this.invalidate();
	}
	
	public void zoomOut( float x, float y)
	{
		if( !setOffsets )
		{
			int [] location = new int[2];;
			this.getLocationOnScreen(location);
			viewOffsetX = (float)location[0];
			viewOffsetY = (float)location[1];
			setOffsets = true;
		}
		
		int hitColumn = (int)( (x - this.x - viewOffsetX ) / ( this.tileWidth * scaleFactor ) );
		int hitRow = (int)( (y - this.y - viewOffsetY ) / ( this.tileHeight * scaleFactor ) );
		
		scaleFactor = 0.5f;
		this.x = lastZoomedOutX;// x - this.viewOffsetX - hitColumn * scaleFactor * tileWidth;
		this.y = lastZoomedOutY; //y - this.viewOffsetY - hitRow * scaleFactor*tileHeight;;
		matrix.reset();
		matrix.postScale(scaleFactor, scaleFactor);
		matrix.postTranslate(this.x,this.y);
		zoomedIn = false;
		this.invalidate();
	}
	
	protected Map gameMap;
	protected int relativeX;
	protected int relativeY;
	protected Bitmap currentWorldView;
	private boolean setOffsets = false;
	private float viewOffsetX;
	private float viewOffsetY;

	public void moveCharacter( float onScreenHitX, float onScreenHitY )
	{
		if( !setOffsets )
		{
			int [] location = new int[2];;
			this.getLocationOnScreen(location);
			viewOffsetX = (float)location[0];
			viewOffsetY = (float)location[1];
			setOffsets = true;
		}
		
		int hitColumn = (int)( (onScreenHitX - x - viewOffsetX ) / ( this.tileWidth * scaleFactor ) );
		int hitRow = (int)( (onScreenHitY - y - viewOffsetY ) / ( this.tileHeight * scaleFactor ) );
		Log.d("MapView", String.format("col,row = %d,%d", hitColumn, hitRow ));
		characterIsMoving = true;
		destinationColumn = hitColumn;
		destinationRow = hitRow;
	}
		
	public MapView(Context context) {
		super(context);
		tileHeight = 32;
		tileWidth = 32;
		gameMap = new Map( context, 5, 5, 20, 20, tileWidth, tileHeight, 1, 1, 0, 0 );
		relativeX = 300;
		relativeY = 300;

//		updateTime = System.currentTimeMillis();

		lastZoomedOutX = x = 5;
		lastZoomedOutY = y = 30;

		this.setBackgroundColor(Color.BLACK);
		regionMap = gameMap.getCurrentRegion().getRegionBitmap();
		scaleFactor = (float)0.5;
		matrix = new Matrix();
		matrix.postScale(scaleFactor, scaleFactor);
		matrix.postTranslate(x, y);

		zoomedIn = false;
	}
}
